<#
run-all.ps1

一键运行脚本（PowerShell）。
作用：
  - 依次运行 Cangjie 基准（通过现有的 bench\run-cj-bench.ps1）
  - 运行 Node 参考实现基准（通过 node-ref\bench\run-bench.ps1）
  - 收集输出并把源码/脚本打包到一个时间戳目录，方便开源复现

用法（在项目根目录的 PowerShell 中运行）：
  # 设置仓颉运行命令，例如 'cjpm run'
  $env:CANGJIE_RUN_CMD = 'cjpm run'
  # 运行一键脚本
  .\bench\one-click\run-all.ps1

先决条件：
  - Windows PowerShell
  - Node.js 可用（用于运行 node 的参考实现与简单 bench 脚本）
  - 如果使用 Cangjie，需在系统中可用的 `cjpm` / `cjo` 工具或将 `CANGJIE_RUN_CMD` 指向一个可执行命令
#>

Set-StrictMode -Version Latest

$ts = (Get-Date).ToString('yyyyMMdd-HHmmss')
$workDir = (Resolve-Path .).Path
$outDir = Join-Path $workDir "bench\one-click-output-$ts"
if (-not (Test-Path $outDir)) { New-Item -ItemType Directory -Path $outDir | Out-Null }

Write-Host "Packaging run artifacts into: $outDir"

function Run-ScriptAndCapture($scriptPath, $outName) {
    if (-not (Test-Path $scriptPath)) { Write-Host "找不到脚本： $scriptPath" -ForegroundColor Yellow; return $null }
    Write-Host "Running: $scriptPath"
    try {
        $out = & $scriptPath 2>&1
    } catch {
        $out = "SCRIPT FAILED: $_"
    }
    $file = Join-Path $outDir $outName
    $out | Out-File -FilePath $file -Encoding UTF8
    Write-Host "Saved output to: $file"
    return $file
}

# 1) Run Cangjie bench (uses existing bench\run-cj-bench.ps1)
$cjScript = Join-Path $workDir 'bench\run-cj-bench.ps1'
if ($env:CANGJIE_RUN_CMD) {
    Write-Host "Detected CANGJIE_RUN_CMD: $env:CANGJIE_RUN_CMD"
    $cjOut = Run-ScriptAndCapture $cjScript "cj-run-$ts.log"
} else {
    Write-Host "未设置 CANGJIE_RUN_CMD，跳过 Cangjie 运行（若要运行，请先设置 $env:CANGJIE_RUN_CMD = 'cjpm run'）" -ForegroundColor Yellow
}

# 2) Run Node bench (node-ref\bench\run-bench.ps1)
$nodeScript = Join-Path $workDir 'node-ref\bench\run-bench.ps1'
$nodeOut = Run-ScriptAndCapture $nodeScript "node-run-$ts.log"

# 3) Copy useful files into package (README, scripts, src, cjpm.toml, node-ref)
Write-Host "Copying project files to package dir..."
$itemsToCopy = @(
    'README_CJ.md',
    'cjpm.toml',
    'src',
    'vendor',
    'node-ref',
    'bench\run-cj-bench.ps1',
    'node-ref\bench\simple-bench.js',
    'bench\one-click\run-all.ps1'
)
foreach ($i in $itemsToCopy) {
    $src = Join-Path $workDir $i
    if (Test-Path $src) {
        $dest = Join-Path $outDir (Split-Path $i -Leaf)
        try {
            if ((Get-Item $src).PsIsContainer) {
                Copy-Item -Path $src -Destination $outDir -Recurse -Force
            } else {
                Copy-Item -Path $src -Destination $dest -Force
            }
            Write-Host "Copied: $i"
        } catch { Write-Host "Copy failed: $i -> $_" -ForegroundColor Yellow }
    }
}

# 4) Save a small manifest describing the run and outputs
$manifest = @{
    timestamp = $ts
    cj_output = if ($cjOut) { (Get-Item $cjOut).FullName } else { $null }
    node_output = if ($nodeOut) { (Get-Item $nodeOut).FullName } else { $null }
    notes = 'If you plan to publish this package, include vendor binaries (OpenSSL) if required on Windows.'
}
$manifest | ConvertTo-Json -Depth 5 | Out-File -FilePath (Join-Path $outDir 'manifest.json') -Encoding UTF8

Write-Host "One-click run complete. Package is at: $outDir"
Write-Host "To reproduce locally: set `$env:CANGJIE_RUN_CMD and run this script from project root:`
Write-Host "  `$env:CANGJIE_RUN_CMD = 'cjpm run' ; .\bench\one-click\run-all.ps1"

Write-Host 'Done.'
